define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'common/jszip',"common/FileSaver"], function ($, undefined, Backend, Table, Form,Jszip,FileSaver) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/index' + location.search,
                    table: 'report',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'status', title: __('Status'),operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        {field: 'service_type', title: __('Service_type'),operate:false, searchList: {"0":__('Service_type 0'),"1":__('Service_type 1'),"2":__('Service_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'statementtime', title: __('Statementtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [{
                                name: 'see',
                                title: __('查看服务报告'),
                                classname: 'btn btn-xs btn-primary',
                                text: __('查看服务报告'),   icon: '',
                                extend:' target="_blank"',
                                url: function (row,index) {
                                    return row["report"];
                                }
                            }],events: Table.api.events.operate,formatter: Table.api.formatter.operate,
                        }
                    ]

                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $("#downloadreport").on("click",function(res){

                var zip = new Jszip();
                var img = zip.folder("images"); // 生成图片文件夹
                var all = table.bootstrapTable('getSelections'); // 获取表格全部选中的数据
                if(all.length < 1){
                    Layer.msg("请选择要下载的二维码");
                    return false;
                }
                var ids=0;
                var idstatus=false;
                if(all.length==1){
                    var url = all[0].report;                            // 获取图片地址
                    var a = document.createElement('a');          // 创建一个a节点插入的document
                    var event = new MouseEvent('click')           // 模拟鼠标click点击事件
                    a.download = "服务报告.png"                // 设置a节点的download属性值
                    a.href = url;                                 // 将图片的src赋值给a节点的href
                    a.dispatchEvent(event)                        // 触发鼠标点击事件
                }else{
                    for(var i=0; i< all.length ;i++)
                    {
                        if(all[i].report_base64){
                            var img_arr = all[i].report_base64.split(',');
                            img.file("服务报告： "+(i+1)+".png", img_arr[1], {base64:true});//图片批量写入文件
                        }
                    }
                    zip.generateAsync({type:"blob"}).then(function(content) {
                        // see FileSaver.js
                        saveAs(content, "example.zip");
                    });
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});